package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.commom.Result;
import com.example.demo.entity.Book;
import com.example.demo.service.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    @PostMapping
    public Result<?> save(@RequestBody Book book) {
        bookService.save(book);
        return Result.success();
    }
    
    @PutMapping
    public Result<?> update(@RequestBody Book book) {
        bookService.update(book);
        return Result.success();
    }

    // 批量删除
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        bookService.deleteBatch(ids);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        bookService.deleteById(id);
        return Result.success();
    }
    
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(defaultValue = "") String search1,
                             @RequestParam(defaultValue = "") String search2,
                             @RequestParam(defaultValue = "") String search3) {
        // 如果所有搜索条件都为空，使用简单搜索
        if (search1.isEmpty() && search2.isEmpty() && search3.isEmpty()) {
            Page<Book> bookPage = bookService.findPage(pageNum, pageSize, "");
            return Result.success(bookPage);
        }
        
        // 否则使用多条件搜索
        Page<Book> bookPage = bookService.advancedSearch(pageNum, pageSize, search1, search2, search3);
        return Result.success(bookPage);
    }
}
